﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CellPlacementController : MonoBehaviour {
	// 单例模式
	private static CellPlacementController m_Instance = null;
	public static CellPlacementController Instance {
		get {
			if(m_Instance == null) {
				m_Instance = GameObject.FindObjectOfType(typeof(CellPlacementController)) as CellPlacementController;
			}

			if (!m_Instance) {
				GameObject obj = new GameObject("CellPlacementController");
				m_Instance = obj.AddComponent<CellPlacementController>();
			}
			return m_Instance;
		}
	}


	[Tooltip("细胞")]
	public GameObject[] Cells;

	public Sprite[] CellSprites;

	public Transform Tip;

	// 当前需要放置的细胞
	private GameObject m_CurrentCell;

	private float m_DetectRange;

	private Camera m_MainCamera;
	private Ray m_Ray;
    private RaycastHit m_Hit;
	public Text EnergyText;
	public Image CellImage;
	private Vector3 m_Position;
	private bool m_ReadyToPlace;

	private int m_EnergyValue;
	public GameObject placeWavePrefab;
	public GameObject placeWave;

	private bool m_CanReplace;

	private void Awake() {
		m_MainCamera = Camera.main;

		Tip.gameObject.SetActive(false);
		m_ReadyToPlace = false;
		m_CanReplace = true;
	}

	private void Update() {
		if(m_CanReplace){
			m_CurrentCell = Cells[GameUIController.Instance.CurrentCellIndex];
			CellImage.sprite = CellSprites[GameUIController.Instance.CurrentCellIndex];
			m_DetectRange = m_CurrentCell.GetComponent<Cell>().DetectRange;

			PlaceCell(m_CurrentCell);
		}
	}

	public void PlaceCell(GameObject cell) {
		if (Input.GetMouseButtonDown(0)) {
			
			// 主相机屏幕点转换为射线
			m_Ray = m_MainCamera.ScreenPointToRay(Input.mousePosition);

			//射线碰到了物体
			if (Physics.Raycast(m_Ray, out m_Hit)) {
				m_Position = new Vector3(m_Hit.point.x, m_Hit.point.y, 0f);
				Tip.gameObject.SetActive(true);
				m_ReadyToPlace = true;
			} else {
				m_ReadyToPlace = false;
			}
		}

		if(m_ReadyToPlace) {
			if (Input.GetMouseButton(0)) {
				// UI跟随鼠标移动
				Tip.position = Input.mousePosition;

				// 主相机屏幕点转换为射线
				m_Ray = m_MainCamera.ScreenPointToRay(Input.mousePosition);
				//射线碰到了物体
				if (Physics.Raycast(m_Ray, out m_Hit)) {
					m_Position = new Vector3(m_Hit.point.x, m_Hit.point.y, 0f);
				} else {
					m_ReadyToPlace = false;
				}
				
				// 搜寻最近的物体
				Collider2D[] colliders = Physics2D.OverlapCircleAll(m_Position, m_DetectRange);
				float distance = m_DetectRange;
				float temp;
				foreach (var item in colliders) {
					if(item.CompareTag("Virus")) {
						temp = Vector3.Magnitude(m_Position - item.transform.position);
						if(temp < distance) {
							distance = temp;
						}
					}
				}

				m_EnergyValue = Mathf.RoundToInt(GetValue(distance));
				EnergyText.text = "" + m_EnergyValue;
			}

			if (Input.GetMouseButtonUp(0)) {
				Tip.gameObject.SetActive(false);

				// 主相机屏幕点转换为射线
				m_Ray = m_MainCamera.ScreenPointToRay(Input.mousePosition);

				//射线碰到了物体
				if (Physics.Raycast(m_Ray, out m_Hit)) {
					Vector3 position = new Vector3(m_Hit.point.x, m_Hit.point.y, 0f);
					if(GameController.Instance.ConsumeEnergy(m_EnergyValue)) {
						Instantiate(cell, position, Quaternion.identity);
					    PlaceWave(position);
					}
				}
			}
		}
	}

	private float GetValue(float distance) {
		int energy = m_CurrentCell.GetComponent<Cell>().ConsumedEnergy;
		return (m_DetectRange - distance) / m_DetectRange * energy + energy;
	}

	private void PlaceWave(Vector3 pos) {
        if(placeWave == null) {
			Debug.Log("ins");
			placeWave = Instantiate(placeWavePrefab, pos, Quaternion.identity);
			placeWave.GetComponent<Animator>().Play("WaveAnimation_New2",0,0f);
		}
		else {
			placeWave.transform.position = pos;
			placeWave.GetComponent<Animator>().Play("WaveAnimation_New2",0,0f);
		}
		GetComponent<AudioSource>().Play();
	}

	public void DisablePlacement() {
		m_CanReplace = false;
	}
}
